$(".tabBarBox").load("../../common-html/resource-topTab.html");
var comPageNum = 0;//分页公共页码
var isEdit = false;//是否编辑--判断是否跟新页码到首页
$("#form-member").validate({
    rules: {
        username:"titleCheck",
    },
    //提交表单数据
    submitHandler:function(form){
        //获取
        queryDataFn();
    }
})
function queryDataFn() {
    var category_id=$("#category_id").val(),
        logmax=$("#logmax").val(),
        logmin=$("#logmin").val(),
        shaixuan=$("#shaixuan").val()
    te=$("#text").val();
    console.log(category_id)
    //提交
    var user_data = {
        category_id:category_id,
        created_at_end:logmax,
        created_at_start:logmin,
        state:shaixuan,
        title:te,
    }
    console.log(user_data)
    //获取返回的数据
    var succCallBack = function (data, status, response) {
        console.log(data)
        if(data.errorCode=="0") {
            var dataSource = new Array();
            var table;   //table对象
            dataSource = [];
            var result=data.data
            $.each(result, function (i, field){
                var rowData = new Array();
                //活动标题-0
                rowData.push(field.title);
                //发布时间 1
                rowData.push(field.state);
                //状态 2
                rowData.push(field.floor);
                //创建人 3
                rowData.push(field.created_name);
                //创建时间-4
                rowData.push(field.created_at);
                //创建人-5
                rowData.push(field.category_id);
                //创建时间-6
                rowData.push(field.resource_id);
                //地址-image_url-7
                rowData.push(field.image_url);
                //8
                rowData.push(field.image_path);
                dataSource.push(rowData);
            });
            initDataTable(dataSource);
            return false
        }else{
            //报错
            console.log("123")
            var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
            dttable.fnClearTable(); //清空一下table
            return false
        }
    };
    //给一个接口地址提交数据
    //192.168.0.107:7040
    return web.doAjax(root.sroreinter.search_resource_conf, 'post', user_data, succCallBack);
    //获取数据之后如何展现在页面上
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "fnDrawCallback" : function(){
                if(!isEdit){
                    comPageNum = getDataTablePageNum(dttable);
                }
                //权限控制函数
                power()
            },
            "autoWidth": false,
            "order": [[ 2, "asc" ]],
            "ordering": true,
            "searching": true,
            "paging": true,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu":[10,20,30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function(data,type,row,meta){
                        if(row[1]=='21'){
                            var val='<span>生效</span>'
                        }else if(row[1]=='22'){
                            var val='<span>失效</span>'
                        }
                        return val;
                    },
                    "targets": 1
                },
                {

                    "render": function(data,type,row,meta){
                        // if (row[1]=='21'){
                        //     var val ='<a><i class="Hui-iconfont mr-10" onclick="update_state('+row[1]+','+row[6]+')">关闭</i></a> <a><i class="Hui-iconfont mr-10" onclick="edit_data(\''+row[0]+'\','+row[2]+','+row[6]+',\''+row[7]+'\',\''+row[8]+'\')">&#xe6df;</i></a> <a><i class="Hui-iconfont" onclick="delete_data('+row[6]+')">&#xe6e2;</i></a>';
                        //     return val;
                        // }
                        //<a><i class="Hui-iconfont mr-10" onclick="update_state('+row[1]+','+row[6]+')">开启</i></a>
                        var val ='<a class="powerButton 457"><i class="Hui-iconfont mr-10" onclick="edit_data(\''+row[0]+'\','+row[2]+','+row[6]+',\''+row[7]+'\',\''+row[8]+'\')">&#xe6df;</i></a> <a class="powerButton 458"><i class="Hui-iconfont" onclick="delete_data('+row[6]+')">&#xe6e2;</i></a>';
                        return val;
                    },
                    "targets": 5
                },
            ]
        });
        dttable.fnPageChange(comPageNum);
        isEdit = false;
        //权限控制函数
        power()
    };
}
function update_state(state,id){
    layer.confirm('是否要改变该楼层状态',function(index){
        //此处请求后台程序，下方是成功后的前台处理……
        $.ajax({
            type: "post",
            url: root.sroreinter.update_floor,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                resource_id:id,
                state:state
            },
            beforeSend: function(request) {
                request.setRequestHeader("Apikey", '4rete564sfdsfsd5fsa');
            },
            //传入组装的参数
            success: function (data,user_data,succCallBack) {
                console.log(data)
                if(data.errorCode=="0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                    setTimeout( function () {
                        location.replace("../../operation_resource/floor-html/floor.html");
                    },700);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }
        })
    });
}

function delete_data(id){
    layer.confirm('是否要删除该楼层',function(index){
        //此处请求后台程序，下方是成功后的前台处理……
        $.ajax({
            type: "post",
            url: root.sroreinter.delete_resource_conf,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                resource_id:id,
            },
            beforeSend: function(request) {
                request.setRequestHeader("Apikey", '4rete564sfdsfsd5fsa');
            },
            //传入组装的参数
            success: function (data,user_data,succCallBack) {
                console.log(data)
                if(data.errorCode=="0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                    setTimeout( function () {
                        isEdit = true;
                        queryDataFn();
                    },700);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }

        })

    });

}

function edit_data(title,floor,resource_id,image_url,image_path){
    var index = layer.open({
        type: 2,
        title:"楼层编辑配置",
        content: "floor_edit.html"+"?resource_id="+resource_id+"&floor="+floor+"&title="+title+"&image_url="+encodeURIComponent(encodeURIComponent(image_url))+"&image_path="+encodeURIComponent(encodeURIComponent(image_path))
    });
    layer.full(index);
}
/*
下拉框的展示
 */
$(function () {
    $.ajax({
        type: "post",
        url: root.sroreinter.get_category,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            category_id:1,type:101
        },  //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#category_id").html(interText(dataInter));
                console.log(dataInter)
                $.each(dataInter,function(inx,data2){
                    if(data2.name=="楼层配置"){
                        var member='<option selected value="'+data2.category_id+'" >'+data2.name+'</option>';
                        $('#category_id').append(member)
                    }else {
                        var member='<option value="'+data2.category_id+'" >'+data2.name+'</option>';
                        $('#category_id').append(member)
                    }
                })

            }
        }
    })
})
function discount() {
    $("#category_id").change(function(event) {
        var fanx = $(this).find("option:selected");
        //通栏配置 3 4 5 10
        if(fanx.val()==3){
            window.location.href="../resource-html/resource.html?value="+"拉幕配置"
        }
        if(fanx.val()==4){
            window.location.href="../resource-html/resource.html?value="+"浮框配置"
        }
        if(fanx.val()==5){
            window.location.href="../resource-html/resource.html?value="+"弹框配置"
        }
        if(fanx.val()==2){
            window.location.href="../top-html/top.html"
        }
        //轮播图配置
        if(fanx.val()==6){
            window.location.href="../picture-html/picture.html"
        }
        //搜索栏配置
        if(fanx.val()==7){
            window.location.href="../search-html/search.html"
        }
        //品类展示区配置
        if(fanx.val()==18){
            window.location.href="../display-html/display.html"
        }
        if(fanx.val()==21){
            window.location.href="../categorylogo-html/categorylogo.html"
        }
        if(fanx.val()==22){
            window.location.href="../brandhalllogo-html/brandhalllogo.html"
        }
    });
}

